package com.hyb.hybmodulessearch.controller;

import cn.easyes.core.biz.EsPageInfo;
import cn.easyes.core.conditions.LambdaEsQueryWrapper;
import com.hyb.hybmodulessearch.domain.TribeResponseVo;
import com.hyb.hybmodulessearch.domain.TribeSearchVo;
import com.hyb.hybmodulessearch.mapper.es.TribeMapper;
import com.ruoyi.common.core.constant.TribeConstant;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.system.api.domain.Tribe;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/tribe")
public class TribeSearchController {

    @Autowired
    TribeMapper tribeMapper;

    @InnerAuth
    @PostMapping("/insert")
    public R<Tribe> insert(@RequestBody Tribe tribe){

        TribeResponseVo tribeResponseVo = new TribeResponseVo();
        BeanUtils.copyProperties(tribe,tribeResponseVo);
        tribeResponseVo.setUserCount(0L);
        tribeResponseVo.setArticleCount(0L);
        //处理文章类型
        if (tribe.getLevel().equals(TribeConstant.LEVEL0)){
            tribeResponseVo.setLevel(TribeConstant.LEVEL0_STRING);
        }else if (tribe.getLevel().equals(TribeConstant.LEVEL1)){
            tribeResponseVo.setLevel(TribeConstant.LEVEL1_STRING);
        }else{
            tribeResponseVo.setLevel(TribeConstant.LEVEL2_STRING);
        }



        tribeMapper.insert(tribeResponseVo);

        return R.ok();
    }
    @InnerAuth
    @PostMapping("/update")
    public R<Tribe> update(@RequestBody Tribe tribe){


        TribeResponseVo tribeResponseVo = new TribeResponseVo();
        BeanUtils.copyProperties(tribe,tribeResponseVo);
        //处理文章类型
        if (tribe.getLevel()!=null){
            if (tribe.getLevel().equals(TribeConstant.LEVEL0)){
                tribeResponseVo.setLevel(TribeConstant.LEVEL0_STRING);
            }else if (tribe.getLevel().equals(TribeConstant.LEVEL1)){
                tribeResponseVo.setLevel(TribeConstant.LEVEL1_STRING);
            }else if (tribe.getLevel().equals(TribeConstant.LEVEL2)){
                tribeResponseVo.setLevel(TribeConstant.LEVEL2_STRING);
            }
        }

        tribeMapper.updateById(tribeResponseVo);

        return R.ok();
    }

    @GetMapping("/search")
    public AjaxResult search(TribeSearchVo tribeSearchVo){

        LambdaEsQueryWrapper<TribeResponseVo> tribe= new LambdaEsQueryWrapper<>();

        if (!StringUtils.isEmpty(tribeSearchVo.getContent())){
            tribe.match(TribeResponseVo::getDes,tribeSearchVo.getContent())
                    .or().like("name.keyword",tribeSearchVo.getContent())
                    .or().eq("number.keyword",tribeSearchVo.getContent())
                    .or().like("type.keyword",tribeSearchVo.getContent())
                    .or().like("level.keyword",tribeSearchVo.getContent())
                    .or().like("label.keyword",tribeSearchVo.getContent());
        }
        EsPageInfo<TribeResponseVo> pageInfo = tribeMapper.pageQuery(tribe, tribeSearchVo.getPageNum(), tribeSearchVo.getPageSize());
        Map<String,Object> map=new HashMap<>();
        map.put("total",pageInfo.getTotal());
        map.put("pages",pageInfo.getPages());
        map.put("list",pageInfo.getList());
        return AjaxResult.success().put("map",map);
    }


}
